import  UserModel  from '@/model/User';

import OrderModel from "@/model/Order";
import ProductModel from "@/model/Product";
import { Op } from "sequelize";

export default class Order{
    /**
     * name
     */
    public async getOrderList(ctx,next) {
        try {
            const user_id = ctx.request.headers.token
            const userInfo = await UserModel.findOne({
                where:{
                   user_id:user_id
                }
            })
            const productIdArray = await OrderModel.findAll({
                where:{
                    user_id:user_id
                }
            });
            let productInfo = Array<any>()
            for (let index = 0; index < productIdArray.length; index++) {
                const element = productIdArray[index];
                let data = await ProductModel.findOne({
                    where:{
                        product_id:element.dataValues.product_id
                    }
                })
                productInfo.push({...data?.dataValues,order_id:element.dataValues.order_id})
            }
         
            
            const reslut = {
                productInfo,
                username:userInfo?.dataValues.username
            }
        ctx.body={
            code: 200,
            message: "查询成功",
            data: reslut
        }
        } catch (error) {
         ctx.body={
            code: 400,
            message: "查询失败"
         }   
        }
    }

    public async createOrder(ctx,next) {
        try {
            const user_id = ctx.request.headers.token;

            const {product_id} = ctx.request.query;
            console.log(product_id);
            await OrderModel.create({
                product_id: product_id,
                user_id:user_id
            });
        ctx.body={
            code: 200,
            message: "创建成功",
            data: []
        }
        } catch (error) {
         ctx.body={
            code: 400,
            message: "查询失败"
         }   
        }
    }

}